package com.example.analysis.service.impl;

import com.example.analysis.entity.Quest;
import com.example.analysis.mapper.EvaluateListStudentDao;
import com.example.analysis.mapper.EvaluateSupervisionDao;
import com.example.analysis.mapper.QuestDao;
import com.example.analysis.service.EvaluationStudentService;
import com.example.analysis.utils.ReliabilityAnalysis;
import com.example.analysis.vo.ReliabilityVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EvaluationStudentServiceImpl implements EvaluationStudentService {
    @Resource
    private EvaluateListStudentDao evaluateListStudentDao;
    @Resource
    private EvaluateSupervisionDao evaluateSupervisionDao;
    @Resource
    private QuestDao questDao;
    @Override
    public ReliabilityVO reliabilityAnalysis(String type) {
        // 查询问卷
        List<Quest> questList = questDao.selectQuest(type);
        // 获取每个问题的分数
        List<double[]> allItemScores = new ArrayList<>();
        System.out.println(type);
        if ("1".equals(type)) {
            for (Quest q: questList) {
                double[] itemScores = evaluateListStudentDao.selectScoreByQuestId(q.getQuestId());
                System.out.println(itemScores.length);
                allItemScores.add(itemScores);
            }
        } else {
            for (Quest q: questList) {
                List<Double> itemScores = evaluateSupervisionDao.selectScoreByQuestId(q.getQuestId());
                Double[] array = itemScores.toArray(itemScores.toArray(new Double[0]));
                System.out.println(q.getQuestId() + ", " + array.length);
                allItemScores.add(transform(array));
            }
        }
        ReliabilityVO reliabilityVO = ReliabilityAnalysis.analysis(allItemScores);
        for (int i = 0; i < questList.size(); i++) {
            // 填写题目描述
            reliabilityVO.getReliabilityItemVOList().get(i).setItemDescription(questList.get(i).getQuestEvaluate());
        }
        return reliabilityVO;
    }
    // 转换类型
    private double[] transform(Double[] data) {
        double[] ret = new double[data.length];
        for (int i = 0; i < data.length; i++) {
            ret[i] = data[i] == null ? 0 : data[i];
        }
        return ret;
    }
}
